var myApp = angular.module('villageApp', []);

myApp.controller('villageCtrl', ['$scope', '$timeout', '$http', function($scope, $timeout, $http) {
	$scope.heroes = [];
   $scope.quests = [];
   $scope.selectedHero = null;

   var pollHeroes = function() {
   	$timeout(function() {
   		$http.get('http://localhost:4080/heroes').
   			success(function(data, status, headers, config) {
               for (i = 0; i < data.length; i++) {
   				  $scope.heroes[i] = data[i];
               }
               if ($scope.selectedHero == null) {
                  $scope.selectedHero = data[0];
               }
   			}).
   			error(function(data, status, headers, config) {
   				$scope.heroes = [];
               $scope.selectedHero = null;
   			});
   		pollHeroes();
   	}, 1000);
   };

   pollHeroes();
}]);